<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;
use think\captcha\facade\Captcha;
class Login extends BaseController{
    /*定义一个初始数组*/
    protected $info = array(
        'code'=>200,
        'msg'=>'提示',
        'total'=>0,
        'data'=>[]
    );

    //写入操作日志
    protected function logData($action='',$admin_id=0){
        $pt['time'] = time();
        $pt['admin_id'] =  $admin_id;
        $pt['action'] =  trim($action);
        Db::table('an_admin_log')->insert($pt);
    }

    public function index(){
        if(session('?adminData')){
            return $this->fetch('index/index');
        }else{
            return $this->fetch();
        } 
    }

    // 登录后台
    public function loginAdmin(){
        $pt = input("post.");
        $captcha = new Captcha();
        if($captcha::check($pt['code'])){
            $where = [
                'user'=>trim($pt['user']),
                'pass'=>md5('anan'.md5(trim($pt['pass'])))
            ];
            $adminData = Db::table('an_admin')
            ->where($where)
            ->field('id,name,user,login_time,role_id')
            ->select()
            ->toArray(); 
            if(count($adminData) > 0){
                $this->logData('登录系统',$adminData[0]['id']);
                session('adminData', serialize($adminData[0]));
                Db::table('an_admin')->where('id', $adminData[0]['id'])->data(['login_time'=>time()])->update();
                $this->info['code'] = 200;
                $this->info['msg'] = '登录成功';
                $this->info['data'] = $adminData;
                return json($this->info);
            }else{
                $this->info['code'] = 201;
                $this->info['msg'] = '账号或密码有误';
                return json($this->info);
            }
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '验证码有误'.$pt['code'];
            return json($this->info);
        }
    }

    public function login(){
        $pt = input("post.");
        $where = [
            'user'=>trim($pt['user']),
            'pass'=>md5('anan'.md5(trim($pt['pass'])))
        ];
        $adminData = Db::table('tb_admin')
        ->alias('a')
        ->join(['tb_admin_role'=>'b'],'a.role_id = b.id')
        ->where($where)
        ->field('a.id,name,user,role_name,login_time,role_id')
        ->select()
        ->toArray(); 
        if(count($adminData) > 0){
            if(!captcha_check($pt['vercode'])){
                $this->info['code'] = 201;
                $this->info['msg'] = '验证码错误';
                return json($this->info);
            }else{
                $this->logData('登录系统',$adminData[0]['id']);
                session('adminData', serialize($adminData[0]));
                Db::table('tb_admin')->where('id', $adminData[0]['id'])->data(['login_time'=>time()])->update();
                $this->info['code'] = 200;
                $this->info['msg'] = '登录成功';
                $this->info['data'] = $adminData;
                return json($this->info);
            };
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '账号或密码有误';
            return json($this->info);
        }

    }

    public function outLogin(){
        $this->logData('退出系统',unserialize(session('adminData'))['id']);
        session('adminData', null);
        return redirect('/admin/login/index');
    }

    public function cuowu(){
        return $this->fetch();
    }
}
